﻿using System;
using System.Collections.Generic;
using System.Text;
using ThoughtWorks.QRCode.Codec.Data;
using ThoughtWorks.QRCode.Codec.Util;
using ThoughtWorks.QRCode.Codec;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace DotNet.Utilities.二维码
{
    //(V-1)*4+21 最终生成的尺寸
    //version包含字符数量有限，设置为0为自动调整
    public class TqrCode
    {
        /// <summary>
        /// 生成二维码图片
        /// </summary>
        /// <param name="sQRText">要转换的字符串</param>
        /// <param name="iSize">二维码图片的尺寸</param>
        /// <param name="oImg">返回的二维码的图片</param>
        /// <returns>返回处理结果</returns>
        public bool createQR(string sQRText, int iSize, int iVersion, out Image oImg)
        {
            bool bResult = false;
            oImg = null;
            try
            {
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                qrCodeEncoder.QRCodeBackgroundColor = System.Drawing.Color.White;//设置二维码背景颜色
                qrCodeEncoder.QRCodeForegroundColor = System.Drawing.Color.Black;//设置二维码前景色
                qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                qrCodeEncoder.QRCodeScale = iSize;
                qrCodeEncoder.QRCodeVersion = iVersion;
                qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
                oImg = qrCodeEncoder.Encode(sQRText, Encoding.UTF8);
                //Bitmap oBmp = qrCodeEncoder.Encode(sQRText);
                bResult = true;
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }
        /// <summary>
        /// 生成带logo的二维码图片
        /// </summary>
        /// <param name="sQRText">要转换的字符串</param>
        /// <param name="iSize">二维码图片的尺寸</param>
        /// <param name="oBmp">返回的二维码的图片</param>
        /// <returns>返回处理结果</returns>
        public bool createQR(string sQRText, int iSize, int iVersion, Image oLogo, out Image oImg)
        {
            const float multiple = 4;
            bool bResult = false;
            oImg = null;
            try
            {
                if (!createQR(sQRText, iSize, iVersion, out oImg)) return false;
                if (oLogo != null)
                {
                    float w = oImg.Width / multiple;
                    float pw = w / oLogo.Width;
                    float ph = w / oLogo.Height;
                    if (pw > ph) pw = ph;
                    int mw = (int)pw * oLogo.Width;
                    int mh = (int)ph * oLogo.Height;
                    int x = (oImg.Width - mw) / 2;
                    int y = (oImg.Height - mh) / 2;
                    Graphics oGp = Graphics.FromImage(oImg);
                    oGp.DrawImage(oLogo, x, y, mw, mh);
                    oGp.Dispose();
                }
                bResult = true;
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }

        public bool saveToStream(string sQrText, int iSize, int iVersion, out Stream oStream)
        {
            bool bResult = false;
            oStream = null;
            try
            {
                Image oImg = null;
                if (!createQR(sQrText, iSize, iVersion, out oImg)) return false;
                oImg.Save(oStream, ImageFormat.Png);
                bResult = true;
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }

        /// <summary>
        /// 保存图片到文件
        /// </summary>
        /// <param name="sPath">图片的保存路径</param>
        /// <param name="sQRText">要转换的字符串</param>
        /// <param name="iSize">二维码点尺寸</param>
        /// <param name="iVersion">二维码版本号</param>
        /// <param name="sLogo">logo文件地址</param>
        /// <param name="sFile">文件名字</param>
        /// <returns>返回处理结果</returns>
        public bool saveToFile(string sPath, string sQRText, int iSize, int iVersion, string sLogo, string sFile)
        {
            bool bResult = false;
            try
            {
                Image oImg = null, oLogo = null;
                //文件名称
                if (sPath == "") return false;
                if (sPath.Substring(sPath.Length - 1, 1) != @"\") sPath += @"\";
                if (sQRText == "") return false;
                //保存图片到目录
                if (!Directory.Exists(sPath))//判断是否有此文件夹
                {
                    //当前目录不存在，则创建
                    Directory.CreateDirectory(sPath);//创建此文件夹
                }
                if (File.Exists(sLogo)) oLogo = Image.FromFile(sLogo);
                if (!createQR(sQRText, iSize, iVersion, oLogo, out oImg)) return false;//创建二维码的图片
                oImg.Save(sPath + sFile, ImageFormat.Png);//保存二维码的图片
                bResult = true;
            }
            catch (Exception ex)
            {

            }
            return bResult;
        }
    }
}
